package com.bjtu.literatureclient.service.journal;

import com.bjtu.literatureclient.api.vo.request.JournalCnEditRequest;
import com.bjtu.literatureclient.api.vo.response.JournalCnResponse;
import com.bjtu.literatureclient.api.vo.response.JournalEnResponse;
import com.bjtu.literatureclient.dao.category.ICategoryDao;
import com.bjtu.literatureclient.dao.journal.IJournalDao;
import com.bjtu.literatureclient.dao.user.UserIndexDao;
import com.bjtu.literatureclient.domain.journal.JournalChinese;
import com.bjtu.literatureclient.domain.journal.JournalEnglish;
import com.bjtu.literatureclient.domain.journal.JournalPaperChinese;
import com.bjtu.literatureclient.domain.journal.JournalPaperEnglish;
import com.bjtu.literatureclient.domain.user.User;
import com.bjtu.literatureclient.domain.user.UserIndexNoTable;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("journalService")
public class IJournalServiceImpl implements IJournalService{
	
	@Autowired
	private IJournalDao journalDao;
	@Autowired
	private UserIndexDao userIndexDao;
	@Autowired
	private ICategoryDao categoryDao;

	@Override
	public List<?> queryJournalChinese(String type) {
		if(type.equals("1")){
			return journalDao.queryJournalEnglish();
		}else if(type.equals("2")){
			return journalDao.queryJournalChineseList();
		}
		return null;
	}

	@Override
	public Boolean addJournalPaper(Integer type, Object object,String item, String date) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("userInfo");
		UserIndexNoTable userIndexNo=new UserIndexNoTable();
		userIndexNo.setUserNo(user.getUserNo());
		userIndexNo.setItem(item);
		userIndexNo.setItatus(1);
		userIndexNo.setStamp(date);
		if(type ==2){
			JournalPaperChinese jounalPaperChinese = (JournalPaperChinese) object;
			userIndexNo.setIndexNo(jounalPaperChinese.getIndexNo());
			userIndexNo.setIndexCode("JC");
			userIndexDao.addUserIndexNoTable(userIndexNo);
			System.out.println(userIndexNo);
			return journalDao.addJournalPaper2(jounalPaperChinese);
		}else{
			JournalPaperEnglish jounalPaperEnglish = (JournalPaperEnglish) object;
			userIndexNo.setIndexNo(jounalPaperEnglish.getIndexNo());
			userIndexNo.setIndexCode("JE");
			userIndexDao.addUserIndexNoTable(userIndexNo);
			System.out.println(userIndexNo);
			return journalDao.addJournalPaper1(jounalPaperEnglish);
		}
	}

	@Override
	public List<?> queryJournalPaper(String type,String userNo) {
		if(type.equals("1")){
			return journalDao.queryJournalPaperEnglish(userNo);
		}else if(type.equals("2")){
			return journalDao.queryJournalPaperChinese(userNo);
		}
		return null;
	}
	@Override
	public Object queryJournalPaperByIndexNo(String type, String userNo, String indexNo) {
		if(type.equals("1")){
			return journalDao.queryJournalPaperByIndexNoEnglish(userNo,indexNo);
		}else if(type.equals("2")){
			return journalDao.queryJournalPaperByIndexNoChinese(userNo,indexNo);
		}
		return null;
	}

	@Override
	public List<JournalCnResponse> getJournalCnResponseList(User curUser) {
		List<JournalChinese> journalChineseList = journalDao.queryJournalChineseByIndexNo();
		List<JournalCnResponse> journalCnResponseList = journalChineseList.stream().map(JournalCnResponse::of).collect(Collectors.toList());
		return journalCnResponseList;
	}

	@Override
	public List<JournalEnResponse> getJournalEnResponseList(User curUser) {
		List<JournalEnglish> journalEnglishes = journalDao.queryJournalEnglishList();
		List<JournalEnResponse> journalEnResponseList = journalEnglishes.stream().map(JournalEnResponse::of).collect(Collectors.toList());
		return journalEnResponseList;
	}

	@Override
	public Boolean editJournalEn(JournalEnglish request) {
		Boolean result =  journalDao.updateJournalEn(request);
		return result;
	}


	@Override
	public Boolean editJournalCn(JournalCnEditRequest request) {
		Boolean result =  journalDao.updateJournalCn(createJournalChinese(request));
		return result;
	}

	@Override
	public boolean deleteJournalPaper(String type, String indexNo, User user) {
		String userRole = user.getUserRole();
		if(userRole.equals("4")){//高级用户，逻辑的删除文献，UserIndexNoTable的Status值设置为3，
			// 把文献信息从SubsetIndexNoTable中删除
			userIndexDao.modifyUserIndexNoTable(3,user.getUserNo(),indexNo);
		}else if(userRole.equals("3")){//超级用户
			userIndexDao.deleteUserIndexNoTable(user.getUserNo(),indexNo);
			Date d=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//格式化为日期/时间字符串
			String date = sdf.format(d);
			userIndexDao.addDeletedindexnotable(indexNo,date);
		}
		categoryDao.deletesubsetIndexNo(user.getUserNo(),indexNo);
		return true;
	}

	@Override
	public boolean editJournalPaper(String type, Object journalPaper, String item, String date) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("userInfo");
		UserIndexNoTable userIndexNo=new UserIndexNoTable();
		userIndexNo.setUserNo(user.getUserNo());
		userIndexNo.setItem(item);
		userIndexNo.setIndexCode("J");
		userIndexNo.setItatus(2);
		userIndexNo.setStamp(date);
		if(type.equals("2")){
			JournalPaperChinese jounalPaperChinese = (JournalPaperChinese) journalPaper;
			userIndexNo.setIndexNo(jounalPaperChinese.getIndexNo());
			userIndexDao.editUserIndexNoTable(userIndexNo);
			System.out.println(userIndexNo);
			return journalDao.editJournalPaper2(jounalPaperChinese);
		}else{
			JournalPaperEnglish jounalPaperEnglish = (JournalPaperEnglish) journalPaper;
			userIndexNo.setIndexNo(jounalPaperEnglish.getIndexNo());
			userIndexDao.editUserIndexNoTable(userIndexNo);
			System.out.println(userIndexNo);
			return journalDao.editJournalPaper1(jounalPaperEnglish);
		}
	}

	private JournalChinese createJournalChinese(JournalCnEditRequest request){
		JournalChinese journalChinese = new JournalChinese();
		journalChinese.setIndexNo(request.getIndexNo());
		journalChinese.setJournalName1(request.getJournalName1());
		journalChinese.setJournalName2(request.getJournalName2());
		journalChinese.setiSSN(request.getiSSN());
		journalChinese.setCN(request.getcN());
		journalChinese.setFoundYear(request.getFoundYear());
		journalChinese.setInstitutionID(request.getInstitutionID());
		journalChinese.setJournalEmail(request.getJournalEmail());
		journalChinese.setNote(request.getNote());
		journalChinese.setReleaseCycles(request.getReleaseCycles());
		return journalChinese;
	}
}

